﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Content

Public Class GameBackground

    Public CurrentModifier As Integer

    Private mBackgroundImages As New SortedList(Of Integer, BackgroundImage)
    Private mGraphicsDevice As GraphicsDevice

    Public Sub New()
        Dim bkgBackground As BackgroundImage

        For x As Integer = 1 To 5
            bkgBackground = New BackgroundImage(x, "Background0" + x.ToString)
            mBackgroundImages.Add(x, bkgBackground)
        Next
    End Sub

    Public Overridable Sub Load(ByVal currentContentManager As ContentManager, ByVal currentGraphicsDevice As GraphicsDevice)
        Dim bkgBackground As BackgroundImage
        Dim intLastPositionAvailable As Integer = 0
        Dim decImageScale As Decimal
        mGraphicsDevice = currentGraphicsDevice
        For Each bkgBackground In mBackgroundImages.Values
            bkgBackground.SpriteTexture = currentContentManager.Load(Of Texture2D)(bkgBackground.SpriteName)
            decImageScale = currentGraphicsDevice.Viewport.Height / bkgBackground.SpriteTexture.Height
            bkgBackground.Size = New Rectangle(0, 0, bkgBackground.SpriteTexture.Width * decImageScale, bkgBackground.SpriteTexture.Height * decImageScale)
            bkgBackground.Position = New Vector2(intLastPositionAvailable, 0)
            intLastPositionAvailable += bkgBackground.GetScaledWidth(currentGraphicsDevice)
        Next
    End Sub

    Public Sub Draw(ByVal currentGameTime As GameTime, ByVal currentSpriteBatch As SpriteBatch)
        Dim bkgBackground As BackgroundImage

        For Each bkgBackground In mBackgroundImages.Values
            currentSpriteBatch.Draw(bkgBackground.SpriteTexture, bkgBackground.Position, New Rectangle(0, 0, bkgBackground.SpriteTexture.Width, bkgBackground.SpriteTexture.Height), Color.White, 0, Vector2.Zero, bkgBackground.GetImageScale(mGraphicsDevice), SpriteEffects.None, 0)
        Next
    End Sub

    Public Function GetFieldWidth() As Integer
        Dim bkgBackground As BackgroundImage
        Dim intTotalWidth As Integer = 0
        For Each bkgBackground In mBackgroundImages.Values
            intTotalWidth += bkgBackground.GetScaledWidth(mGraphicsDevice)
        Next
        Return intTotalWidth
    End Function


    Public Const BackgroundMoveSpeed As Integer = 3

    Private mLastBackgroundPosition As Integer

    Private mPosition As Vector2 = Vector2.Zero

    Public Sub Move(ByVal theGameTime As GameTime, ByVal theSpeed As Vector2, ByVal theDirection As Vector2)
        Dim intCumulativeWidth As Integer = 0
        Dim bkgBackground As BackgroundImage
        Dim intLastPositionAvailable As Integer
        Dim intNewBackgroundPosition As Integer
        Dim blnShowingBackground As Boolean = False
        Dim intFirstPosition As Integer = 9999999
        Dim intLastPosition As Integer = 0
        mPosition += theDirection * theSpeed * theGameTime.ElapsedGameTime.TotalSeconds

        For Each bkgBackground In mBackgroundImages.Values
            intNewBackgroundPosition = bkgBackground.Position.X + (mPosition.X * -1)
            bkgBackground.Position = New Vector2(intNewBackgroundPosition, 0)
            If intNewBackgroundPosition + bkgBackground.GetScaledWidth(mGraphicsDevice) > intLastPositionAvailable Then
                intLastPositionAvailable = intNewBackgroundPosition + bkgBackground.GetScaledWidth(mGraphicsDevice)
            End If
        Next

        For Each bkgBackground In mBackgroundImages.Values
            If bkgBackground.Position.X < -(bkgBackground.GetScaledWidth(mGraphicsDevice)) Then
                bkgBackground.Position = New Vector2(intLastPositionAvailable, 0)
            End If
        Next

        For Each bkgBackground In mBackgroundImages.Values
            If bkgBackground.Position.X + bkgBackground.SpriteTexture.Width < 0 Then
                blnShowingBackground = True
            End If
        Next

        If blnShowingBackground = False Then
            For Each bkgBackground In mBackgroundImages.Values
                If bkgBackground.Position.X < intFirstPosition Then
                    intFirstPosition = bkgBackground.Position.X
                End If
            Next
            For Each bkgBackground In mBackgroundImages.Values
                If bkgBackground.Position.X > intLastPosition Then
                    intLastPosition = bkgBackground.Position.X
                End If
            Next
            For Each bkgBackground In mBackgroundImages.Values
                If bkgBackground.Position.X = intLastPosition Then
                    bkgBackground.Position = New Vector2(intFirstPosition - bkgBackground.SpriteTexture.Width, 0)
                End If
            Next
        End If

    End Sub

End Class
